<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="我的余额" style="margin-left: 30px">
        <div style="color: green; font-size: 20px">{{ balancePrice }}元</div>
      </el-form-item>

      <el-form-item label="订单单号" prop="orderNumber">
        <el-input v-model="queryParams.orderNumber" placeholder="请输入订单单号" clearable @keyup.enter="handleQuery"/>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['business:merchantBalance:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="merchantBalanceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="图片" align="center" prop="img">
        <template #default="scope">
          <img style="width: 50px; height: 50px;" :key="scope.row.id" :src="scope.row.img" alt="图片" v-if="scope.row.img != null">
          <span v-if="scope.row.img === null"></span>
        </template>
      </el-table-column>
      <el-table-column label="商户名称" align="center" prop="merchantName" />
      <el-table-column label="订单单号" align="center" prop="orderNumber" />
      <el-table-column label="类型" align="center" prop="type">
        <template #default="scope">
          <span v-if="scope.row.type === 'in'">客户充值</span>
          <span v-if="scope.row.type === 'out'">订单支付</span>
          <span v-if="scope.row.type === 'freightOut'">运费支付</span>
          <span v-if="scope.row.type === 'backIn'">订单退款</span>
        </template>
      </el-table-column>
      <el-table-column label="金额" align="center" prop="amount" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
<!--          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['business:merchantBalance:edit']">修改</el-button>-->
<!--          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['business:merchantBalance:remove']">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商户的余额记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="merchantBalanceRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商户id" prop="merchantId">
          <el-input v-model="form.merchantId" placeholder="请输入商户id" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="订单id" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单id" />
        </el-form-item>
        <el-form-item label="订单单号" prop="orderNumber">
          <el-input v-model="form.orderNumber" placeholder="请输入订单单号" />
        </el-form-item>
        <el-form-item label="金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="图片" prop="img">
          <el-input v-model="form.img" placeholder="请输入图片" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="MerchantBalance">
import { listMerchantBalance, getMerchantBalance, delMerchantBalance, addMerchantBalance, updateMerchantBalance } from "@/api/business/merchantBalance";
import { getMyMerchantBalance } from "@/api/business/merchantCapital";

const { proxy } = getCurrentInstance();

const merchantBalanceList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const balancePrice = ref(0);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderNumber: null,
    type: null,

  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询商户的余额记录列表 */
function getList() {
  loading.value = true;
  listMerchantBalance(queryParams.value).then(response => {
    merchantBalanceList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    merchantId: null,
    userId: null,
    orderId: null,
    orderNumber: null,
    type: null,
    amount: null,
    img: null,
    createTime: null,
    createBy: null
  };
  proxy.resetForm("merchantBalanceRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
  getMyMerchantInfo();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加商户的余额记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getMerchantBalance(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改商户的余额记录";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["merchantBalanceRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateMerchantBalance(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMerchantBalance(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除商户的余额记录编号为"' + _ids + '"的数据项？').then(function() {
    return delMerchantBalance(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('business/admin/merchantBalance/export', {
    ...queryParams.value
  }, `余额记录_${new Date().getTime()}.xlsx`)
}

function getMyMerchantInfo() {

  getMyMerchantBalance().then(response => {

    console.log("-----response------" + response)
    console.log("-----response.rows------" + response.data)
    balancePrice.value = response.data;

  });
}


getList();
getMyMerchantInfo();
</script>
